/**
  * @description: 公用方法
  * @author: foge
  * @update: 刘金榜(2019-8-6)
  */
export default {
    //判断设备
    browserRedirect(){
        var sUserAgent = navigator.userAgent.toLowerCase();
        var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
        var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
        var bIsMidp = sUserAgent.match(/midp/i) == "midp";
        var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
        var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
        var bIsAndroid = sUserAgent.match(/android/i) == "android";
        var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
        var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
        if (  bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
            return 'android';
        }
        else  if (bIsIphoneOs || bIsIpad)
        {
            return 'ios'
        }
        else {
            return 'pc';
        }        
    },
    // 获取url参数输入某个获取某一个参数得到某一个，得不到或输入空返回null
    getQueryString(name){ 
        let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
        let r = window.location.search.substr(1).match(reg); 
        if (r != null) {
            return unescape(r[2]);
        } else {
            return null; 
        }       
    },
    // url传参 最终得到以对象的形式存储 ?id=1&ip=2 => {id:1,ip:2}
    getRequest(){  
        let url = location.search; //获取url中"?"符后的字串  
        let theRequest = new Object();  
        if (url.indexOf("?") != -1) {  
           let str = url.substr(1);  
           let strs = str.split("&");  
           for(var i = 0; i < strs.length; i ++) {  
              theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);  
           }  
        }  
        return theRequest;  
    },
    //调用系统振动
    playSystemSound(){
        let browser = this.browserRedirect();
        if( browser == 'android' ){
            window.android.clickView();
        } 
        else if( browser == 'ios'){
            window.webkit.messageHandlers.setControlButtonVibrate.postMessage(null);            
        }        
    },
    
    
    /*16进制颜色转为RGB格式*/
    getRGB(hex){
        var rgb=[0,0,0];
        if(/#(..)(..)(..)/g.test(hex)){
            rgb=[parseInt(RegExp.$1,16),parseInt(RegExp.$2,16),parseInt(RegExp.$3,16)];
        };
        return rgb.join(",");
    },
    //js_RGB转16进制(rgb2hex)
    colorRGB2Hex(color) {
        let rgb = color.split(',');
        let hex ='#' + ("00000" + (rgb[0] << 16 | rgb[1] << 8 | rgb[2]).toString(16)).slice(-6);
        return hex;
    },
    //时间处理
    buildDate (date) {
        if (typeof date == 'string') {
          date = date.replace(/-/g, '/');
          if (date.indexOf('/') >= 0) {
            date = new Date(date);
          } else {
            if (!isNaN(parseInt(date))) {
              date = new Date(parseInt(date));
            }
          }
        } else if (typeof date == 'number') {
          // 给的是秒
          if ((date + '').length <= 10) {
            date = date * 1000;
          }
          date = new Date(date);
        } else if (!(date instanceof Date)) {
          return false;
        }
        return date;
    },
    //格式化时间戳
    formatDate (dd, fmt) {
        var date = this.buildDate(dd);
        if (!date) {
          return dd;
        }
        var o = {
            'y+': date.getFullYear(), //年份
            'M+': date.getMonth() + 1, //月份
            'd+': date.getDate(), //日
            'h+': date.getHours(), //小时
            'm+': date.getMinutes(), //分
            's+': date.getSeconds(), //秒
            'q+': Math.floor((date.getMonth() + 3) / 3), //季度
            S: date.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) {
          fmt = fmt.replace(
            RegExp.$1,
            (date.getFullYear() + '').substr(4 - RegExp.$1.length)
          );
        }      
        for (var k in o) {
          if (new RegExp('(' + k + ')').test(fmt)) {
            fmt = fmt.replace(
              RegExp.$1,
              RegExp.$1.length == 1
                ? o[k]
                : ('00' + o[k]).substr(('' + o[k]).length)
            );
          }
        }
        return fmt;
    },           
    //房间名称以及设备名称正则校验，仅支持中英文及数字
    checkRoomName(name){
        let reg = /^[\u4e00-\u9fa5_a-zA-Z0-9]+$/;
        return reg.test(name);
    },
    //字符串截取:用于房间页的名称限制24个字符或12个中文汉字
    getByteVal(val, max) {
      var returnValue = '';
      var byteValLen = 0;
      for (var i = 0; i < val.length; i++) {
          if (val[i].match(/[^\x00-\xff]/ig) != null)
          byteValLen += 2;
          else
          byteValLen += 1;
          if (byteValLen > max)
          break;
          returnValue += val[i];
      }
      return returnValue;
  }
}
